package com.gitee.wsl.security.signature.subtle


import com.gitee.wsl.ext.string.toByteArray
import com.gitee.wsl.security.signature.KeyPair
import kotlin.io.encoding.Base64
import kotlin.test.Test
import kotlin.test.assertNotNull

class Tests {

    @Test
    fun ed25519() {
        val keys = KeyPair.newKeyPair()

        // sign a msg
        val msg = "moin moin".encodeToByteArray()
        val signer = Ed25519Sign(keys.privateKey)
        val signature = signer.sign(msg)


        // encode
        val privateKeyAsString = Base64.encode(keys.privateKey)
        assertNotNull(privateKeyAsString)
        val publicKeyAsString = Base64.encode(keys.publicKey)
        assertNotNull(publicKeyAsString)


        // decode
        val privateKey = Base64.decode(privateKeyAsString)
        assertNotNull(privateKey)
        val publicKey = Base64.decode(publicKeyAsString)

        // verify with public key
        val verifier = Ed25519.verify(msg,signature,publicKey)
        //verifier.verify(signature, msg) // throws exception in case is not verified
    }
}